<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="门店id" prop="shopId">
        <el-input
          v-model="queryParams.shopId"
          placeholder="请输入门店id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="门店名称" prop="bokaName">
        <el-input
          v-model="queryParams.bokaName"
          placeholder="请输入门店名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="补发师别名" prop="aliasName" label-width="100px">
        <el-input
          v-model="queryParams.aliasName"
          placeholder="请输入补发师别名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="补发师姓名" prop="nickName" label-width="100px">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入补发师姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="所属军区" prop="theirMilitary">
        <el-select
          v-model="queryParams.theirMilitary"
          placeholder="请选择所属军区"
        >
          <el-option
            v-for="dict in belong_military_region"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            @change="handleQuery"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="在职" prop="jobStatus">
        <el-select
          v-model="queryParams.jobStatus"
          placeholder="请选择是否在职"
        >
          <el-option
            v-for="dict in is_jobstatus"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
            @change="handleQuery"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:hairFixTeacher:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:hairFixTeacher:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:hairFixTeacher:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:hairFixTeacher:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="refresh"
          @click="updateTeacher"
          :loading="syncLoading"
          v-hasPermi="['business:hairFixTeacher:sync']"
        >同步博卡老师数据</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="hairFixTeacherList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="补发师ID" align="center" prop="id" v-if="true" />
      <el-table-column label="门店ID" align="center" prop="shopId" /> -->

      <el-table-column label="补发师名字" align="center" prop="nickName" />
      <el-table-column label="补发师别名" align="center" prop="aliasName" />
      <el-table-column label="员工编号" align="center" prop="bokaEmpId" />
      <el-table-column label="补发师头像" align="center" prop="avatar">
        <template #default="scope">
          <template v-if="scope.row.avatar != null">
            <template v-for="item in scope.row.avatar.split(',')" :key="item">
              <!--              <image-preview :src="item" :width="50" :height="50" />-->
              <el-avatar :src="item"></el-avatar>
            </template>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phonenumber" />

      <el-table-column label="门店名称" align="center" prop="bokaName">
        <template #default="scope">
          <template v-for="item in hairFixShopList" :key="item">
            <span v-if="item.id === scope.row.shopId">{{ item.bokaName }}</span>
          </template>
        </template>
      </el-table-column>

      <el-table-column label="所属军区" align="center" prop="theirMilitary" />
      <el-table-column label="在职状态" align="center" prop="jobStatus" />
      <el-table-column label="是否显示" align="center" prop="status" >
        <template #default="scope">
          <el-switch v-if="scope.row.status!=null" v-model="scope.row.status" @change="statusChange($event,scope.row)" />
        </template>
      </el-table-column>

      <!-- <el-table-column label="补发师背景" align="center" prop="backImg">
        <template #default="scope">
          <template v-if="scope.row.backImg != null">
            <template v-for="item in scope.row.backImg.split(',')" :key="item">
              <image-preview :src="item" :width="50" :height="50" />
            </template>
          </template>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="微信二维码" align="center" prop="wechatQrCode">
        <template #default="scope">
          <template v-if="scope.row.wechatQrCode != null">
            <template
              v-for="item in scope.row.wechatQrCode.split(',')"
              :key="item"
            >
              <image-preview :src="item" :width="50" :height="50" />
            </template>
          </template>
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        label="从业年限"
        align="center"
        prop="workingSeniority"
      /> -->
      <!-- <el-table-column label="座右铭" align="center" prop="motto" /> -->
      <el-table-column
        prop="orderNum"
        label="排序"
        width="60"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <div>
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="toHairTeacherEvaluate(scope.row)"
              v-hasPermi="['business:hairFixTeacher:edit']"
            >
              查看补发师评价列表
            </el-button>
          </div>
          <div>
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['business:hairFixTeacher:edit']"
              >修改</el-button
            >
          </div>
          <div>
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['business:hairFixTeacher:remove']"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改补发师信息对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form
        ref="hairFixTeacherRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="补发师姓名" prop="nickName">
          <el-input
            v-model="form.nickName"
            placeholder="请输入补发师姓名"
            style="width: 215px"
          />
        </el-form-item>

        <el-form-item label="补发师别名" prop="aliasName">
          <el-input
            v-model="form.aliasName"
            placeholder="请输入补发师别名"
            style="width: 215px"
          />
        </el-form-item>

        <el-form-item label="手机号码" prop="phonenumber">
          <el-input
            v-model="form.phonenumber"
            placeholder="请输入手机号码"
            maxlength="11"
            style="width: 215px"
          />
        </el-form-item>
        <el-form-item label="选择门店" prop="shopId">
          <!-- <el-input v-model="form.shopId" placeholder="请输入门店id" /> -->
          <el-select v-model="form.shopId" value-key="id" filterable>
            <el-option
              v-for="item in hairFixShopList"
              :key="item.id"
              :label="item.bokaName"
              :value="item.id"
          /></el-select>
        </el-form-item>

        <el-form-item label="标签" prop="tags">
          <el-select
            v-model="form.tags"
            multiple
            filterable
            allow-create
            default-first-option
            placeholder="请选择标签"
          >
            <el-option
              v-for="dict in sys_teacher_tag"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="补发师头像" prop="avatar">
              <image-uploads v-model="form.avatar" :limit="1" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="所属军区" prop="theirMilitary">
          <el-select v-model="form.theirMilitary" placeholder="请选择所属军区">
            <el-option
              v-for="dict in belong_military_region"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              @change="handleQuery"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="从业年限" prop="workingSeniority">
          <el-input-number v-model="form.workingSeniority" controls-position="right"
                    :min="0"
                    style="width: 215px"/>
        </el-form-item>
        <!-- <el-form-item label="座右铭" prop="motto">
          <el-input v-model="form.motto" placeholder="请输入座右铭" />
        </el-form-item> -->
        <el-form-item label="显示排序" prop="orderNum">
          <el-input-number
            v-model="form.orderNum"
            controls-position="right"
            :min="0"
            style="width: 215px"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="HairFixTeacher">
import {
  listHairFixTeacher,
  getHairFixTeacher,
  delHairFixTeacher,
  addHairFixTeacher,
  updateHairFixTeacher,
  updateBokaTeacher
} from "@/api/business/hairFixTeacher";
import { listHairFixShop } from "@/api/business/hairFixShop";
import { getRoleUser } from "@/api/system/user";
import { useRouter } from "vue-router";

import ImageUploads from "@/components/ImageUploads/index.vue";

const router = useRouter();
const { proxy } = getCurrentInstance();
const { sys_teacher_tag, belong_military_region, is_jobstatus } = proxy.useDict(
  "sys_teacher_tag",
  "belong_military_region", "is_jobstatus"
);
const hairFixTeacherList = ref([]); //补发师列表
const hairFixShopList = ref([]); //门店列表
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const teacherUserList = ref([]);
const syncLoading = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    shopId: undefined,
    miniUserId: undefined,
    nickName: undefined,
    theirMilitary: undefined,
    avatar: undefined,
    wechatQrCode: undefined,
    workingSeniority: undefined,
    motto: undefined,
    shopName: undefined, //门店名称模糊查询
    aliasName: undefined
  },
  rules: {
    id: [{ required: true, message: "主键id不能为空", trigger: "blur" }],
    shopId: [{ required: true, message: "门店id不能为空", trigger: "blur" }],
    nickName: [{ required: true, message: "昵称不能为空", trigger: "blur" }],
    avatar: [
      { required: true, message: "补发师头像不能为空", trigger: "blur" },
    ],
    phonenumber: [
      {
        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
        message: "请输入正确的手机号码",
        trigger: "blur",
      },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

getHairFixShopList();
/** 查询门店列表 */
function getHairFixShopList() {
  loading.value = true;
  listHairFixShop({ pageNum: 1, pageSize: 99999 }).then((response) => {
    hairFixShopList.value = response.rows;
  });
}
// 理发师列表
getList();
// getRoleUserList()
/** 根据角色key查询用户列表 */
// function getRoleUserList(){
//   getRoleUser({"roleKey": "teacher"}).then(response =>{
//     teacherUserList.value =  response.data
//   })
//
// }

/**搜索门店列表 */
// function searchHairFixShopList(query) {
//   if (query) {
//     loading.value = true;
//     setTimeout(() => {
//       loading.value = false;
//       hairFixShopList.value = hairFixShopList.value.filter((item) => {
//         return item.shopName.toLowerCase().includes(query.toLowerCase());
//       });
//     }, 200);
//   } else {
//     hairFixShopList.value = [];
//   }
// }

function toHairTeacherEvaluate(row) {
  console.log(row);
  // this.$router.push({ name: 'hairTeacherEvaluate', query: { teacherId: row.id } });

  router.push({
    path: "/gallantry/hairTeacherEvaluate",
    query: { teacherId: row.id },
  });
}

/** 查询补发师信息列表 */
function getList() {
  loading.value = true;
  listHairFixTeacher(queryParams.value).then((response) => {
    response.rows.forEach((item) => {
      if(item.status!=null){
        item.status = item.status == 0 ? true : false
      }
    });
    hairFixTeacherList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    shopId: null,
    miniUserId: null,
    nickName: null,
    theirMilitary: null,
    avatar: null,
    wechatQrCode: null,
    workingSeniority: null,
    motto: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
  };
  proxy.resetForm("hairFixTeacherRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加补发师信息";
}
function statusChange(e,a){
  let data = JSON.parse(JSON.stringify(a))
  data.status = e ? 0 : 1;
  proxy.$modal.loading("正在修改...");
  updateHairFixTeacher(data).then((response) => {
    proxy.$modal.msgSuccess("修改成功");
    a.status = e
  }).finally(() => {
    proxy.$modal.closeLoading();
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true;
  reset();
  const _id = row.id || ids.value;
  getHairFixTeacher(_id).then((response) => {
    loading.value = false;
    form.value = response.data;
    if (response.data.tags != null) {
      form.value.tags = response.data.tags.split(",");
    }

    open.value = true;
    title.value = "修改补发师信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["hairFixTeacherRef"].validate((valid) => {
    if (form.value.tags != null) {
      form.value.tags = form.value.tags.toString();
    }

    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateHairFixTeacher(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      } else {
        // console.log(form.value);
        addHairFixTeacher(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除补发师信息编号为"' + _ids + '"的数据项？')
    .then(function () {
      loading.value = true;
      return delHairFixTeacher(_ids);
    })
    .then(() => {
      loading.value = true;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {})
    .finally(() => {
      loading.value = false;
    });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "business/hairFixTeacher/export",
    {
      ...queryParams.value,
    },
    `hairFixTeacher_${new Date().getTime()}.xlsx`
  );
}

/** 请求博卡同步接口 */
function updateTeacher() {
  syncLoading.value = true;
  updateBokaTeacher()
    .then((response) => {
      proxy.$modal.msgSuccess("同步成功");
      syncLoading.value = false;
      getList();
    })
    .finally(() => {
      syncLoading.value = false
      buttonLoading.value = false;
    });
}
</script>
